package com.px.lovepath;

import java.io.File;

import android.content.Context;
import android.os.Environment;

public class LovePathEnv {

	private static LovePathEnv mSingleton = null;
	private static Context mContext;
	
	private static final String APPLICATION_NAME = "LovePath";
	private static final String DIR_CACHE = File.separator + "Cache";
	private static final String DIR_AVATAR = File.separator + "Avatar";
	// ### 方法 ####
	public synchronized static void startUp(Context context) {
		if (mSingleton == null) {
			mSingleton = new LovePathEnv(context);
		}
	}
	public static LovePathEnv get() {
		assert mSingleton != null;
		return mSingleton;
	}
	/**
	 * 获取应用程序数据存储目录.
	 * 
	 * @return 数据存储目录.
	 */
	public synchronized File getAppDataDirectory() {
		File dir = new File(Environment.getExternalStorageDirectory(), APPLICATION_NAME);
		ensureDirectoryExists(dir);
		return dir;
	}
	/**
	 * 获取应用程序内部数据库目录.
	 * 
	 * @return 应用程序内部数据库目录.
	 */
	public synchronized File getDatabaseDirectory() {
		return mContext.getDatabasePath("name").getParentFile();
	}
	/**
	 * 获取缓存目录.
	 * 
	 * @return 缓存目录.
	 */
	public synchronized File getCacheDirectory() {
		File dir = new File(getAppDataDirectory(), DIR_CACHE);
		ensureDirectoryExists(dir);
		return dir;
	}
	/**
	 * 获取头像缓存目录
	 * @return
	 */
	public synchronized File getAvatarDirectory() {
		File dir = new File(getCacheDirectory(), DIR_AVATAR);
		ensureDirectoryExists(dir);
		return dir;
	}
	
	// ### 构造函数 ###
	private LovePathEnv(Context context) {
		mContext = context;
	}
	// ### 实现函数 ###
	private void ensureDirectoryExists(File directory) {
		assert directory != null;
		directory.mkdirs();
	}

}
